package com.other;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Objects;

/**
 * Created by baiguantao on 2017/12/5.
 */
public class DateUtils {
    /**日期格式化*/
    private static String parternDefault="yyyy-MM-dd HH:mm:ss";

    /**
     * 时间差值  天
     * 如果 compareDate比baseDate大则 返回大于0
     * @param compareDate 比较时间
     * @param baseDate 基数时间
     *
     */
    public static int getDay(Date compareDate,Date baseDate){
        int i =-1;
        long diff = compareDate.getTime() - baseDate.getTime();
        diff=diff/1000/60/60/24;
        i=Integer.valueOf(String.valueOf(diff));
        return i;
    }


    /**
     * 根据字符串 转成Date
     *
     */
    public  static  Date getDateByString(String date,String partern){
        if (Objects.isNull(partern)) partern=parternDefault;
        SimpleDateFormat sdf=new SimpleDateFormat(partern);
        try {
            return sdf.parse(date);
        } catch (ParseException e) {
            System.err.print("转换错误,请认真检查您的日期字符串格式");
        }
        return null;
    }

    /**
     * 根据字符串 转成Date
     *
     */
    public  static  Date getDateByString(String date){
        return getDateByString(date,null);
    }


    /**
     * 根据 Date-->字符串
     *
     */
    public  static  String getDateString(Date date,String partern){
        if (Objects.isNull(partern)) partern=parternDefault;
        SimpleDateFormat sdf=new SimpleDateFormat(partern);
        try {
            return sdf.format(date);
        } catch (Exception e) {
            System.err.print("转换错误,请认真检查您的日期字符串格式");
        }
        return null;
    }

    /**
     * 根据 Date-->字符串
     *
     */
    public  static  String getDateString(Date date){
        return getDateString(date,null);
    }

    /**
     * now时间是否在begin和end之间
     * betweenDate("2017-07-03 10:00:00","2017-07-20 17:00:00",null);
     * */
    public  static  boolean betweenDate(String beginDate2,String endDate2,Date now){
        boolean flag=false;
        try {
            SimpleDateFormat sdf=new SimpleDateFormat(parternDefault);
            Date beginDate= sdf.parse(beginDate2);
            Date endDate= sdf.parse(endDate2);

            if (now==null) {
                now=new Date();
            }
            if (beginDate!=null&&endDate!=null&&(beginDate.getTime()<endDate.getTime())) {
                if (beginDate.getTime()<now.getTime()&&now.getTime()<endDate.getTime()) {
                    flag=true;
                }
            }
        } catch (ParseException e) {
            e.printStackTrace();
            return  flag;
        }
        return flag;
    }

    public static void main(String[] args) {
//        int i=getDay(getDateByString("2017-10-17 17:34:04"),new Date());
//        System.out.println(i);
        Date date=getDateByString("2017-07-17 17:34:04");
        System.out.println(getDateString(date));

        boolean flag=betweenDate("2017-07-03 10:00:00", "2017-07-20 17:00:00", date);
        System.out.println(flag);
    }
}
